package com.wx.app.card.config;

import com.wx.app.card.dao.entity.UserEntity;
import com.wx.app.card.dao.mapper.UserMapper;
import com.wx.app.card.domain.vo.UserInfo;
import com.wx.app.card.utils.CacheUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 类功能描述：
 *
 * @author：刘富国
 * @createTime：2019/8/3 9:43
 */
@Component
public class TokenInterceptor implements HandlerInterceptor {

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {

        UserInfo token = CacheUtils.getUserInfo(request.getHeader("token"));
        if (token == null) {
            throw new RuntimeException("无效的token！");
        }
        UserEntity user = userMapper.selectById(token.getUserId());
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        return true;

    }

}
